package com.bobi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.bobi.entity.PurchaseOrder;
import com.bobi.enums.ResultCode;
import com.bobi.mapper.PurchaseOrderMapper;
import com.bobi.service.PurchaseOrderService;
import com.bobi.utils.PageUtil;
import com.bobi.utils.ResultUtil;
import com.bobi.vo.PageVO;
import com.bobi.vo.ResultMessage;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author wenyuan
 * @version 1.0
 * @date 2023/7/4
 * @since 8
 */
@RestController
@RequestMapping("/purchaseOrder")
public class PurchaseOrderController {

    @Autowired
    private PurchaseOrderService purchaseOrderService;

    /**
     * 分页查询所有信息
     * @param pageVO 分页对象
     * @return 返回分页对象
     */
    @GetMapping(value=  "/page")

    public ResultMessage<Page<PurchaseOrder>> page(@RequestBody PurchaseOrder purchaseOrder, PageVO pageVO){
        if(purchaseOrder == null){
            return ResultUtil.error(ResultCode.PURCHASEORDER_SEARCH_ERROR);
        }
        QueryWrapper<PurchaseOrder> queryWrapper = purchaseOrder.initWrapper    (purchaseOrder);
        Page<PurchaseOrder> page = PageUtil.initPage(pageVO);
        Page<PurchaseOrder> list = new Page<>();
        if(queryWrapper == null){
            list = purchaseOrderService.page(page);
        }else{
            list = purchaseOrderService.page(page, queryWrapper);
        }
        return ResultUtil.data(list);
    }

    /**
     * 添加进货单信息
     * @param purchaseOrder 进货单对象
     * @return 返回的标志
     */
    @PostMapping
    public ResultMessage<Object> addActivity(PurchaseOrder purchaseOrder){
        // 如果删除成功
        if(purchaseOrderService.save(purchaseOrder)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultCode.PURCHASEORDER_ADD_ERROR);
    }

    /**
     * 更新活动
     * @param purchaseOrder 进货单对象
     * @return 返回对象
     */
    @PutMapping
    public ResultMessage<Object> updateActivity(PurchaseOrder purchaseOrder){
        // 构造查询条件包装
        UpdateWrapper<PurchaseOrder> updateWrapper = new UpdateWrapper<PurchaseOrder>().eq("id", purchaseOrder.getId());
        // 如果更新成功
        if(purchaseOrderService.update(purchaseOrder, updateWrapper)){
            return ResultUtil.success();
        }
        return ResultUtil.error(ResultCode.PURCHASEORDER_UPDATE_ERROR);
    }

    /**
     * 删除进货单(逻辑删除)
     * @param id 活动id
     * @return 结果消息
     */
    @DeleteMapping("/{id}")
    public ResultMessage<Object>  deleteActivity(@PathVariable("id") String id){
        if(id == null){
            return ResultUtil.error(ResultCode.PURCHASEORDER_DELETE_ERROR);
        }
        purchaseOrderService.removeById(id);
        return ResultUtil.success();
    }

    @DeleteMapping
    public ResultMessage<Object>  deleteBatchActivity(@RequestParam List<String> ids){
        if(ids == null){
            return ResultUtil.error(ResultCode.PURCHASEORDER_DELETE_ERROR);
        }
        purchaseOrderService.removeBatchByIds(ids);
        return ResultUtil.success();
    }




}